﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using GCalendar = Google.GData.Calendar;
using GExtensions = Google.GData.Extensions;
using System.Diagnostics;

namespace GSync
{
    class GoogleAppointmentsProvider : GoogleDataProvider<GCalendar.EventEntry, GCalendar.EventFeed, GCalendar.CalendarService>
    {
        protected override string FeedUri
        {
            get
            {/*http://www.google.com/calendar/feeds/jo@gmail.com/private/full*/
                return
                    @"http://www.google.com/calendar/feeds/" +
                    GSyncFactory.Instance.SettingsManager.ServerAccountName +
                    @"/private/full";
            }
        }

        protected override Google.GData.Calendar.EventFeed Feed
        {
            get
            {

                if (_Feed == null)
                {
                    GCalendar.EventQuery _SelectQuery = new GCalendar.EventQuery(FeedUri);
                    _SelectQuery.UseSSL = true;
                    _SelectQuery.NumberToRetrieve = 200;
                    _SelectQuery.StartDate = DateTime.Today.Subtract(new TimeSpan(14, 0, 0, 0));
                    _Feed = Service.Query(_SelectQuery);
                }
                return _Feed;
            }
        }

        protected override Google.GData.Calendar.CalendarService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = new Google.GData.Calendar.CalendarService(GSyncFactory.Instance.SettingsManager.AppVersion);

                    _Service.setUserCredentials(GSyncFactory.Instance.SettingsManager.ServerAccountName,
                                                GSyncFactory.Instance.SettingsManager.ServerPassword);
                }

                return _Service;
            }
        }

        protected override void CopyToNative(InternalEntry entry, Google.GData.Calendar.EventEntry newNativeEntry)
        {
            InternalAppointment appointment = entry as InternalAppointment;


            newNativeEntry.Title.Text = appointment.Subject;
            newNativeEntry.Content.Content = appointment.Notes;

            // Set a location for the event.
            GExtensions.Where eventLocation = new GExtensions.Where();
            eventLocation.ValueString = appointment.Location;
            newNativeEntry.Locations.Add(eventLocation);

            //create single-occurence event
            if (!appointment.Recurrence.IsRecurrent)
            {
                DateTime start = appointment.AllDayEvent ? appointment.BeginDate.Date : appointment.BeginDate;
                DateTime end = appointment.AllDayEvent ? appointment.EndDate.Date : appointment.EndDate;

                GExtensions.When eventTime = new GExtensions.When(start, end);
                eventTime.AllDay = appointment.AllDayEvent;

                newNativeEntry.Times.Add(eventTime);
            }
            // recurrent event
            else
            {
                /*GExtensions.Recurrence recurrence =  new GExtensions.Recurrence();
                recurrence.Value = appointment.Recurrence.ICalSpec;
                nativeEntry.Recurrence = recurrence;*/

                throw new NotImplementedException("Sorry, but recurrent events aren't supported yet. Please wait for the next version of the programm.");
            }



        }

        protected override InternalEntry ConvertToInternal(Google.GData.Calendar.EventEntry nativeEntry)
        {

                InternalAppointment appointment = new InternalAppointment();


                appointment.Subject = nativeEntry.Title.Text;
                appointment.Notes = nativeEntry.Content.Content;

                appointment.Location = nativeEntry.Locations.Count > 0 ? nativeEntry.Locations[0].ValueString : string.Empty;

                appointment.ID = nativeEntry.Id.AbsoluteUri.Substring(nativeEntry.Id.AbsoluteUri.LastIndexOf('/') + 1);

                // single-occurence event
                if (nativeEntry.Times.Count == 1)
                {
                    appointment.BeginDate = nativeEntry.Times[0].StartTime;
                    appointment.EndDate = nativeEntry.Times[0].EndTime;
                    appointment.AllDayEvent = nativeEntry.Times[0].AllDay;
                }
                // recurrent event
                else
                {
                    appointment.CanSynchronize = false;
                    //throw new NotImplementedException("Sorry, but recurrent events aren't supported yet. Please wait for the next version of the programm.");

                }

                return appointment;
        }



    }
}
